<?php

class Backoffice_HelpdeskController extends CC_Controller
{
    public function indexAction()
    {
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/css/jquery.treeview.css');
        
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/tiny_mce/jquery.tinymce.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery-ui.min.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.iutil.pack.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.idrag.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.grid.columnSizing.pack.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.treeview.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/tiny_mce/tiny_mce.js');
    }
    
    public function providerInboxAction()
    {
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/css/jquery.treeview.css');
        
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery-ui.min.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.iutil.pack.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.idrag.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.grid.columnSizing.pack.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.treeview.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/tiny_mce/tiny_mce.js');
    }
    
    public function newTicketAction()
    {
        $newTicketForm = new Backoffice_Model_FormNewTicket($this->_em);
        $this->view->isAjax = false;
        
        
        if($this->_getParam('ajax') == '1')
        {
            $this->_helper->getHelper('layout')->disableLayout();
            $this->view->isAjax = true;
        }
        
        $this->view->form = $newTicketForm;
    }
    
    public function newCommentAction()
    {
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Backoffice_Model_FormNewComment($this->_em);
        
        if($this->getRequest()->isPost())
    	{
    	    if($form->isValid($this->_request->getParams()))
    	    {
    	        /** 
                 * @var CC\Service\HelpdeskService 
                 */
    	        $helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
            	$helpdeskService->addUserComment($form->getValue('message'), $this->_helper->userSession()->getId(), $form->getValue('ticketId'));
            	
            	$res = new stdClass();
            	$res->success = true;
            	
            	$this->_helper->viewRenderer->setNoRender();
            	echo Zend_Json_Encoder::encode($res);
    	    }
    	}
    	else
    	{
    	    $form->setAttrib('id', 'newCommentForm');
            $this->view->form = $form;
    	}
    }
    
    public function esculateTicketAction()
    {
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Backoffice_Model_FormEsculateTicket($this->_em);
        $form->setAttrib('id', 'esculateTicketForm');
        $request = $this->getRequest();
        
        if($request->isPost())
    	{
    	    if($form->isValid($request->getParams()))
    	    {
    	        /** 
                 * @var CC\Service\HelpdeskService 
                 */
    	        $helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
    	        
    	        $emailAddress = CC_Inflector::extractEmailAddresses($request->getParam('user'));
    	        $emailAddress = $emailAddress[0];
    	        
    	        $userId = $helpdeskService->getUserIdFromEmail($emailAddress, $this->_helper->userSession()->getId());
    	        
            	if($helpdeskService->esculateTicket($request->getParam('ticketId'), $userId, $request->getParam('message')))
            	{
                    $res = new stdClass();
                	$res->success = true;
                	
                	$this->_helper->viewRenderer->setNoRender();
                	echo Zend_Json_Encoder::encode($res);
            	}
            	
            	
    	        //$this->_helper->viewRenderer->setNoRender();
    	        //echo Zend_Json_Encoder::encode(print_r($this->_request->getParams(), true));
    	    }
    	}
    	else
    	{
    	    $form->setAttrib('id', 'esculateTicketForm');
            $this->view->form = $form;
    	}
    }
    
    public function newFolderAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Backoffice_Model_FormNewFolder($this->_em);
        
    	if($this->getRequest()->isPost())
    	{
    	    if($form->isValid($this->_request->getParams()))
    	    {
                /** 
                 * @var CC\Service\HelpdeskService 
                 */
            	$helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
            	
    	        $responce = new stdClass();
    	        
    	        if($helpdeskService->createFolder($this->_helper->userSession()->getId(), $this->_request->getParam('folder'), $this->_request->getParam('name')))
    	            $responce->success = true;
    	        else
    	            $responce->success = false;
	            
	            echo Zend_Json_Encoder::encode($responce);
    	    }
    	}
    	else 
    	{
            $form->setAttrib('id', 'newFolderForm');
            echo $form;
    	}
    }
    
    public function folderListingAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        
        Zend_Controller_Front::getInstance()->unregisterPlugin('ZFDebug_Controller_Plugin_Debug');
        
        if($this->getRequest()->getParam('folder') !== null)
        {
            $helpdesk = $this->_serviceLocator->getService('HelpdeskService');
            $tickets = $helpdesk->getUserTickets($this->_helper->userSession()->getId(), $this->getRequest()->getParam('folder'));
            
            if(count($tickets))
                echo Zend_Json_Encoder::encode($tickets);
        }
        else
        {
            echo Zend_Json_Encoder::encode(array());
        }
    }
    
    public function listFoldersAction()
    {
        /** @var CC\Service\HelpdeskService */
    	$helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
        $this->_helper->getHelper('layout')->disableLayout();
        
        $this->view->folderListArray = $helpdeskService->getUserFolders($this->_helper->userSession()->getId());
    }
    
    public function deleteAction()
    {
        /** @var CC\Service\HelpdeskService */
        $helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        
        $deleteSuccess = $helpdeskService->deleteFolder($this->_helper->userSession()->getId(), 
                                                        $this->getRequest()->getParam('folder'));
        
        $responce = new stdClass();
        $responce->success = $deleteSuccess;
        
        echo Zend_Json_Encoder::encode($responce);
    }
    
    public function viewTicketAction()
    {
        /** 
         * @var CC\Service\HelpdeskService 
         */
        $helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
        Zend_Controller_Front::getInstance()->unregisterPlugin('ZFDebug_Controller_Plugin_Debug');
        $this->_helper->getHelper('layout')->disableLayout();
        
        $this->view->ticket = $helpdeskService->getTicket(
            $this->getRequest()->getParam('ticket'), 
            $this->_helper->userSession()->getId()
        );
    }
    
    public function viewTicketCommentsAction()
    {
        /** 
         * @var CC\Service\HelpdeskService 
         */
        $helpdeskService = $this->_serviceLocator->getService('HelpdeskService');
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        
        $ticket = $helpdeskService->getTicket($this->getRequest()->getParam('ticket'), $this->_helper->userSession()->getId());
        echo Zend_Json_Encoder::encode($ticket);
    }
    
    public function viewRulesAction()
    {
        $this->_helper->getHelper('layout')->disableLayout();
        Zend_Controller_Front::getInstance()->unregisterPlugin('ZFDebug_Controller_Plugin_Debug');
    }
}

?>